{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1614774577.5890806"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取当前时间戳\n",
    "time.time()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021-03-03 20:29:37'"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取当前时间并且用指定格式显示文本\n",
    "# 使用 time 获取当前时间戳\n",
    "now = int(time.time())\n",
    "timeArray = time.localtime(now)\n",
    "StyleTime = time.strftime('%Y-%m-%d %H:%M:%S', timeArray)\n",
    "StyleTime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021-03-03 20:29:37'"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取当前时间并且用指定格式显示文本\n",
    "# 使用 datetime 获取当前时间，数组格式\n",
    "now = datetime.datetime.now()\n",
    "StyleTime = now.strftime('%Y-%m-%d %H:%M:%S')\n",
    "StyleTime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 3, 3, 20, 29, 37)"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取当前时间并且用指定格式显示时间格式\n",
    "now = datetime.datetime.now()\n",
    "str_time = now.strftime('%Y-%m-%d %H:%M:%S')\n",
    "StyleTime = datetime.datetime.strptime(str_time, '%Y-%m-%d %H:%M:%S')\n",
    "StyleTime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1614772080"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 文本 ——> 时间戳\n",
    "str_time = '2021-03-03 19:48:00'\n",
    "# 转为时间数组\n",
    "timeArray = time.strptime(str_time, \"%Y-%m-%d %H:%M:%S\")\n",
    "# timeArray 可以调用 tm_year （年）等\n",
    "timeArray.tm_year # 2013\n",
    "# 转为时间戳\n",
    "timeStamp = int(time.mktime(timeArray))\n",
    "timeStamp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021/03/03'"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 文本 ——> 文本 （显示格式调整）\n",
    "str_time = '2021-03-03 19:48:00'\n",
    "# 转为时间数组\n",
    "timeArray = time.strptime(str_time, \"%Y-%m-%d %H:%M:%S\")\n",
    "# 转为时间格式\n",
    "StyleTime = time.strftime(\"%Y/%m/%d\", timeArray)\n",
    "StyleTime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021-03-03 19:47:00'"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间戳 ——> 文本\n",
    "# 方法一：使用 time\n",
    "timeStamp = 1614772020\n",
    "timeArray = time.localtime(timeStamp)\n",
    "StyleTime = time.strftime(\"%Y-%m-%d %H:%M:%S\", timeArray)\n",
    "StyleTime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021-03-03 19:48:00'"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间戳 ——> 文本\n",
    "# 方法二：使用 datetime\n",
    "timeStamp = 1614772080\n",
    "dateArray = datetime.datetime.fromtimestamp(timeStamp)\n",
    "StyleTime = dateArray.strftime(\"%Y-%m-%d %H:%M:%S\")\n",
    "StyleTime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021-03-03 11:47:00'"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间戳 ——> 文本\n",
    "# 方法三：使用 datetime，指定 utc 时间，相差 8 小时\n",
    "timeStamp = 1614772020\n",
    "dateArray = datetime.datetime.utcfromtimestamp(timeStamp)\n",
    "StyleTime = dateArray.strftime(\"%Y-%m-%d %H:%M:%S\")\n",
    "StyleTime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(datetime.datetime(2021, 2, 1, 0, 0),\n",
       " datetime.datetime(2021, 2, 28, 0, 0),\n",
       " datetime.datetime(2021, 3, 3, 20, 29, 37, 728633))"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 文本 ——> 时间格式\n",
    "startDate = '2021-02-01'\n",
    "endDate = '2021-02-28'\n",
    "\n",
    "startTime = datetime.datetime.strptime(startDate, '%Y-%m-%d')\n",
    "endTime = datetime.datetime.strptime(endDate, '%Y-%m-%d')\n",
    "\n",
    "now = datetime.datetime.now()\n",
    "startTime,endTime,now"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021-02-01 00:00:00'"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间格式 ——> 文本\n",
    "day_str = datetime.datetime.strftime(startTime, '%Y-%m-%d %H:%M:%S')\n",
    "day_str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 3, 3, 19, 47)"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间戳 ——> 时间格式\n",
    "timeStamp = 1614772020\n",
    "timeArray = time.localtime(timeStamp)\n",
    "day_str = time.strftime(\"%Y-%m-%d %H:%M:%S\", timeArray)\n",
    "day_time = datetime.datetime.strptime(day_str, '%Y-%m-%d %H:%M:%S')\n",
    "day_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1614772020"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间格式 ——> 时间戳\n",
    "day_str = datetime.datetime.strftime(day_time, '%Y-%m-%d %H:%M:%S')\n",
    "timeArray = time.strptime(day_str, \"%Y-%m-%d %H:%M:%S\")\n",
    "timeStamp = int(time.mktime(timeArray))\n",
    "timeStamp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021-03-03 19:47:00'"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间格式 ——> 文本\n",
    "day_str = datetime.datetime.strftime(day_time, '%Y-%m-%d %H:%M:%S')\n",
    "day_str"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
